package com.ibangnuo.manage.service.withdrawal;

import com.ibangnuo.framework.domain.CommissionMonth;
import com.ibangnuo.framework.domain.Withdrawal;
import com.ibangnuo.framework.domain.request.withdrawal.WithdrawalRequest;
import com.ibangnuo.framework.model.response.CommonCode;
import com.ibangnuo.framework.model.response.ObjectResult;
import com.ibangnuo.framework.model.response.QueryResponseResult;
import com.ibangnuo.framework.model.response.ResponseResult;
import com.ibangnuo.manage.dao.commission.CommissionRepository;
import com.ibangnuo.manage.dao.withdrawal.WithdrawalRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Optional;

/**
 * @Author: wwy
 * @Date: 2020-03-12 12:10
 */
@Service
public class WithdrawalService {
    @Autowired
    private CommissionRepository commissionRepository;

    @Autowired
    private WithdrawalRepository withdrawalRepository;
    /**
     * 分页条件查询
     * @param page
     * @param size
     * @param withdrawalRequest
     * @return
     */
    public QueryResponseResult findList(int page, int size, WithdrawalRequest withdrawalRequest){
        return null;

    }

    /**
     * 提现添加
     * @param withdrawal
     * @return
     */
    public ResponseResult add(Withdrawal withdrawal){
        withdrawal.setCreateTime(new Date());
        withdrawal.setUpdateTime(new Date());
        withdrawal.setStatus(0);
        CommissionMonth commissionMonth = this.getCommissionMonthById(withdrawal.getOrderTotalMonthId());
        if(commissionMonth == null){
            return new ResponseResult(CommonCode.SUCCESS);
        }
        commissionMonth.setWithdrawalStatus(0);
        commissionRepository.save(commissionMonth);
        withdrawalRepository.save(withdrawal);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    /**
     * 根据佣金id查询
     * @param orderTotalMonthId
     * @return
     */
    public ObjectResult findByOrderTotalMonthId(String orderTotalMonthId) {
        Withdrawal withdrawal = withdrawalRepository.findByOrderTotalMonthId(orderTotalMonthId);
        return new ObjectResult(CommonCode.SUCCESS,withdrawal);
    }

    /**
     *
     * 佣金提现修改
     * @param id
     * @param withdrawal
     * @return
     */
    public ResponseResult update(String id,Withdrawal withdrawal){
        Withdrawal one = this.getWithdrawalById(id);
        CommissionMonth commissionMonthOne = this.getCommissionMonthById(withdrawal.getOrderTotalMonthId());
        if(one != null){
            one.setUpdateTime(new Date());
            one.setStatus(withdrawal.getStatus());
            one.setAlipayNumber(withdrawal.getAlipayNumber());
            one.setBankName(withdrawal.getBankName());
            one.setCardNumber(withdrawal.getCardNumber());
            one.setInvoiceNumber(withdrawal.getInvoiceNumber());
            one.setInvoiceUrl(withdrawal.getInvoiceUrl());
            one.setIsInvoice(withdrawal.getIsInvoice());
            one.setPayee(withdrawal.getPayee());
            one.setPaymentTerm(withdrawal.getPaymentTerm());
            one.setPhone(withdrawal.getPhone());
            one.setTotalCommission(withdrawal.getTotalCommission());
            withdrawalRepository.save(one);
            commissionMonthOne.setWithdrawalStatus(withdrawal.getStatus());
            commissionRepository.save(commissionMonthOne);
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);

    }

    /**
     * 根据id查询Withdrawal
     * @param id
     * @return
     */
    private Withdrawal getWithdrawalById(String id){
        Optional<Withdrawal> optional = withdrawalRepository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    /**
     * 根据id查找月佣金
     * @param id
     * @return
     */
    private CommissionMonth getCommissionMonthById(String id){
        Optional<CommissionMonth> optional = commissionRepository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }
}
